package com.noodles.mall.service.impl;

import com.noodles.mall.entity.Menu;
import com.noodles.mall.mapper.MenuMapper;
import com.noodles.mall.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/** 权限 **/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService, InitializingBean {

    @Autowired(required = false)
    private MenuMapper menuMapper;

    @Override
    public void afterPropertiesSet(){
        INTEGER_MENU_MAP.clear();
        super.list().forEach( menu ->INTEGER_MENU_MAP.put(menu.getId(),menu) );
    }

    /** 增加 **/
    public boolean insert(Menu menu){
        if(super.save(menu)){
            afterPropertiesSet();
            return true;
        }else return false;
    }

    /** 修改: id **/
    public boolean updateById(Menu menu){
        if(super.updateById(menu)){
            afterPropertiesSet();
            return true;
        }else return false;
    }

    /**
     * 查询所有权限
     * @return menuList
     */
    @Override
    public List<Menu> selectWithRole() {
        List<Menu> menuList = menuMapper.selectWithRole();
        return menuList;
    }
}
